package emulator.shell.GUI;

import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import java.util.Map.Entry;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import emulator.hardware.nmos6502.Cpu6502;
import emulator.shell.GUI.menu.WindowConfiguration;
import emulator.shell.GUI.menu.WindowDebug;
import emulator.shell.GUI.menu.WindowIEC;
import emulator.shell.GUI.menu.WindowMemory;
import emulator.shell.GUI.windows.WindowManager;

public class WindowData implements Observer {
	private WindowManager window_manager;
	private ImageIcon menu_selected_icon;
	private ImageIcon menu_unselected_icon;
	private HashMap<Integer, JMenuItem> window_menu_map;
	private ConsoleFrame console;
	public static final int WINDOW_ID_MEMORY = 1;	
	public static final int WINDOW_ID_CONFIGURATION = 2;
	public static final int WINDOW_ID_DEBUG = 3;
	public static final int WINDOW_ID_IECLOG = 4;

	public WindowData(ConsoleFrame console)
	{
		this.console = console;
		window_manager = new WindowManager();
		window_manager.addObserver(this);
    	menu_selected_icon = new ImageIcon("images/menu_selected_icon.png");
    	menu_unselected_icon = new ImageIcon("images/menu_unselected_icon.png");
		window_menu_map = new HashMap<Integer, JMenuItem>();
	}

	void addWindowMenu(JMenu menu, String caption, int window_id) {
		try {
			JMenuItem menu_item = new JMenuItem(caption, menu_unselected_icon);
			menu_item.addActionListener(createListenerForId(window_id));
			window_menu_map.put(window_id, menu_item);
			menu.add(menu_item);
		}
		catch (Exception e)
		{
			System.out.println("Window \""+caption+"\" not available.");
		}
	}

	private ActionListener createListenerForId(int window_id) {
		ActionListener windowListener = null;
		if (window_id == WINDOW_ID_MEMORY)
			windowListener = new WindowMemory(window_manager, window_id, console.getMemory());
		else if (window_id == WINDOW_ID_CONFIGURATION)
			windowListener = new WindowConfiguration(window_manager, window_id, console.getBus());
		else if (window_id == WINDOW_ID_DEBUG)
			windowListener = new WindowDebug(window_manager, window_id, console.getAssembler(), (Cpu6502)console.getCpu(), console.getMemory());
		else if (window_id == WINDOW_ID_IECLOG)
			windowListener = new WindowIEC(window_manager, window_id, console.getIecSim());
		return windowListener;
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		updateWindowStates();
	}

	void updateWindowStates() {
		for (Entry<Integer, JMenuItem> entry : window_menu_map.entrySet())
		{
			if (window_manager.isWindowActive(entry.getKey()))
				entry.getValue().setIcon(menu_selected_icon);
			else
				entry.getValue().setIcon(menu_unselected_icon);
		}
	}
}